<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>reduce()，reduceRight()</title>
</head>

<body>
    <script>
        //reduce
        var arr1 = [1, 2, 3, 4, 5];
        var sum = 0;
        function add() {
            for (var i = 0; i < arr1.length; i++) {
                sum += arr1[i];
                console.log(sum);
            }
        }
        add();
        //reduce_right
        var arr2 = [1, 2, 3, 4, 5];
        var sum2 = 0;
        function add2() {
            for (var i = arr2.length - 1; i >= 0; i--) {
                sum2 += arr2[i];
                console.log(sum2)
            }
        }
        add2();
    </script>
</body>

</html>